package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo;
import sz.szsmk.citizencard.model.network.clarkbean.response.AreaInfo;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/19 9:43
 * description:
 */
public class AppHomeRecommandAdapter extends DelegateAdapter.Adapter<AppHomeRecommandAdapter.AppHomeBannerHolder> implements OnBannerListener {
    private FragmentActivity mContext;
    private LayoutHelper mGridLayoutHelper;


    private ArrayList<String> iconList = new ArrayList<>();
    private ArrayList<String> stringList = new ArrayList<>();
    private List<AdInfo> banner = new ArrayList<>();
    private AreaInfo areaInfo;

    private BaseInterface baseInterface;

    ApphomeBanner adadapter ;





    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, AdInfo adInfo);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    public AppHomeRecommandAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper, int showView) {
        mContext = activity;
        mGridLayoutHelper = headHelper;

    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public AppHomeBannerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View    view = LayoutInflater.from(mContext).inflate(R.layout.apphome_module_recommand_view, parent, false);
        return new AppHomeBannerHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_module_recommand_view;
    }


    @Override
    public void onBindViewHolder(AppHomeBannerHolder holder, int position) {


        if(   holder.banner.getAdapter()==null){
            holder.banner.setAdapter(   adadapter = new ApphomeBanner(banner) );
        }
        holder.banner.getAdapter().setDatas(banner);
        holder.banner.getAdapter().notifyDataSetChanged();
        holder.banner.setOnBannerListener(this);
        if(areaInfo!=null){
            holder.textView.setText(areaInfo.getResource_area_name());
            holder.textViewMore.setText(areaInfo.getResource_sub_title());
            holder.textViewMore.setOnClickListener(v -> {
                JumpUtils.jump(mContext,areaInfo.getTitle_url(),0);
            });
        }

    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public void OnBannerClick(Object data, int position) {
        AdInfo adInfo = banner.get(position);
        if (adInfo != null) {

            String link_url = adInfo.getLink_url();
            int need_header = adInfo.getNeed_header();
            String sys_label = adInfo.getSys_label();
            if (baseInterface != null) {
                baseInterface.onClick(null, link_url, sys_label, need_header,areaInfo.getAdInfoVoList().get(position));
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void setBanner(AreaInfo areaInfo,List<AdInfo> banner) {
        this.areaInfo=areaInfo;
        this.banner.clear();
        banner.sort((x,y) ->Integer.compare(x.getRank(),y.getRank()));
        this.banner.addAll(banner);

//        this.banner.addAll(banner);
    }


    public static class AppHomeBannerHolder extends RecyclerView.ViewHolder {


        private final Banner banner;
        private final TextView textView,textViewMore;
        public AppHomeBannerHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);
            textView=(TextView)itemView.findViewById(R.id.recommand_title);
            textViewMore=(TextView)itemView.findViewById(R.id.recommand_sub_title);


        }
    }

    public class ApphomeBanner extends com.youth.banner.adapter.BannerAdapter<AdInfo, ApphomeBanner.BannerViewHolder>{


        public ApphomeBanner(List<AdInfo> datas) {
            super(datas);
        }

        @Override
        public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(parent.getContext());
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType( ImageView.ScaleType.CENTER_CROP);
            return new BannerViewHolder(parent.getContext() ,imageView,imageView);
        }

        @Override
        public void onBindView(BannerViewHolder holder, AdInfo data, int position, int size) {
            Glide.with(mContext).load(data.getResource_url())
                    .into(holder.iv);
        }

        class BannerViewHolder extends ViewHolder{

            ImageView iv;

            public BannerViewHolder(Context context, View itemView,ImageView iv) {
                super(context, itemView);
                this.iv = iv;
            }
        }
    }


}
